home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Memphis Amiga Group / MAG Disk (1990-09)(Memphis Amiga Group).zip / MAG Disk (1990-09)(Memphis Amiga Group).adf / sMOVIE / source / sMOVIEparse.c < prev    next >
C/C++ Source or Header  |  1989-12-29  |  12KB  |  590 lines

  1. /*    sMOVIEparse.c - Function which scans a text file and builds lists
  2.                      in memory which are subsequently used to produce
  3.                     a view which essentially just smoothly scrolls the
  4.                     text from the file up through the view.
  5.  
  6.                     (Runner up in the 1989 Silver Bogroll award for
  7.                      long involved sentences.)
  8.  
  9.                      M. J. Round.     7 - Oct - 1989.                    */
  10. #include "sMOVIE.h"
  11.  
  12. extern struct GfxBase *GfxBase;
  13.  
  14. extern struct RastPort rp;
  15.  
  16. extern struct TextFont *tf[MAXFONTS+1], *font, *defaultfont;
  17.  
  18. extern char *txptr,fontname[MAXFONTS][MAXFONTNAMELENGTH];
  19.  
  20. extern char line[MAXLINE + 1],buff[MAXLINE * 2];
  21.  
  22. extern int pointsize[MAXFONTS],fonts,textsiz,cmdsiz,maxfontheight;
  23.  
  24. extern int height,width,maxheight,maxwidth,maxdepth,centring,ind,tab;
  25.  
  26. extern int no_text_or_lines;
  27.  
  28. extern short *cmdptr;
  29.  
  30. extern char esc;
  31.  
  32. extern int runningfromcli;
  33.  
  34. void getargs(int,char *);
  35.  
  36. int checksizes(int *,int *,short *,short *,short *);
  37.  
  38. void parse(fp,pass)     /*  parse script file    */
  39.                 
  40. /*  1st pass (pass == 0) Build font table, calculate memory requirement    */
  41. /*  2nd pass (pass != 0) create *txptr and *cmdptr lists.                */
  42.  
  43. FILE *fp;
  44. int pass;
  45. {
  46.     char *p,fname[MAXFONTNAMELENGTH];
  47.     int i,j,fsize,depth;
  48.     FILE *fil;
  49.     
  50.     while((p = fgets(line, MAXLINE, fp)) != NULL) {
  51.  
  52.         if(line[strlen(line)-1] != '\n')
  53.             (void) strcat(line,"\n");
  54.  
  55.         if (*p++ == esc) {
  56.             j =0;
  57.             for (i=0; i<3; i++)
  58.                 /*  code 3 letter command to an integer value    */
  59.                 j = j * 26 - 65 + toupper((int)*p++);
  60.  
  61.             switch (j) {
  62.  
  63. /*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    */
  64.  
  65.                 case 1655:      /*  CLR: clear screen            */
  66.                 case 1656:        /*    CLS: synonym                */
  67.                     if (pass)
  68.                         *cmdptr++ = 7;  /*  command code    */
  69.                     else
  70.                         cmdsiz += 1;
  71.  
  72.                     continue;
  73.  
  74. /*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    */
  75.  
  76.                 case 1727:      /*  COL: change pen colour      */
  77.                     if (pass) {
  78.                         *cmdptr++ = 3;  /*  command code    */
  79.                         getargs(2,p);
  80.                         }
  81.                     else
  82.                         cmdsiz += 3;
  83.  
  84.                     continue;
  85.  
  86. /*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    */
  87.  
  88.                 case 1728:      /*  COM: comment                */
  89.                     continue;
  90.  
  91. /*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    */
  92.  
  93.                 case 2147:      /*  DEP: change screen depth    */
  94.                     if (pass) {
  95.                         p = strtok(stpblk(p)," ;");
  96.                         if (p != NULL && isdigit((int)*p))
  97.                             depth = atoi(p);
  98.                         if (depth > MAX_DEPTH)
  99.                             depth = MAX_DEPTH;
  100.                         if (depth < 1)
  101.                             depth = 1;
  102.                         if (depth > maxdepth)
  103.                             maxdepth = depth;
  104.                         }
  105.  
  106.                     continue;
  107.  
  108. /*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    */
  109.  
  110.                 case 2177:      /*  DFT: default sizesn            */
  111.                     if (pass) {
  112.                         short dummy;
  113.  
  114.                         *cmdptr++ = 13;  /*  command code    */
  115.                         width = GfxBase->NormalDisplayColumns;
  116.                         height = GfxBase->NormalDisplayRows;
  117.                         checksizes(&width,&height,&dummy,&dummy,&dummy);
  118.                         }
  119.                     else
  120.                         cmdsiz += 1;
  121.  
  122.                     continue;
  123.  
  124. /*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    */
  125.  
  126.                 case 2338:      /*  DLY: delay (jiffies)        */
  127.                     if (pass) {
  128.                         *cmdptr++ = 5;  /*  command code    */
  129.                         getargs(1,p);
  130.                         }
  131.                     else
  132.                         cmdsiz += 2;
  133.  
  134.                     continue;
  135.  
  136. /*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    */
  137.  
  138.                 case 3174:      /*  ESC: change esc character    */
  139.                     p = stpblk(p);
  140.                     if (*p != '\0') {
  141.                         esc = *p;
  142.                         if (!pass && runningfromcli) {
  143.                             sprintf(buff,"Escape code changed to '%c'.\n",esc);
  144.                             print(buff);
  145.                             }
  146.                         }
  147.                     else
  148.                         if (!pass && runningfromcli) {
  149.                             sprintf(buff,"Corrupt ESC command:-\n%s\n",line);
  150.                             print(buff);
  151.                             }
  152.  
  153.                     continue;
  154.  
  155. /*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    */
  156.  
  157.                 case 3599:        /*    FIL: include a file            */
  158.                     p = stpblk(p);
  159.                     if (isalpha((int)*p)) {
  160.                         if ((fil = fopen(strtok(p,",; \n"),"r")) == NULL) {
  161.                             if (!pass && runningfromcli) {
  162.                                 sprintf(buff,"FIL can't open %s\n",p);
  163.                                 print(buff);
  164.                                 }
  165.                             }
  166.                         else {
  167.                             /*    isn't recursion wonderful?    */
  168.                             parse(fil,pass);
  169.                             fclose(fil);
  170.                             }
  171.                         }
  172.                     else
  173.                         if (!pass && runningfromcli) {
  174.                             sprintf(buff,"Corrupt FIL command:-\n%s\n",line);
  175.                             print(buff);
  176.                             }
  177.  
  178.                     continue;
  179.  
  180. /*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    */
  181.  
  182.                 case 3737:      /*  FNT: change font            */
  183.                     fname[0] = '\0';
  184.                     /*  fetch name and pointsize    */
  185.                     p = stpsym(stpblk(p),fname,MAXFONTNAMELENGTH);
  186.                     p = stpblk(p);
  187.                     if (isdigit((int)*p))
  188.                         fsize = atoi(p);
  189.                     else
  190.                         fsize = 0;
  191.  
  192.                     if (pass) {
  193.                         *cmdptr++ = 4;  /*  command code    */
  194.                         *cmdptr = 0;    /*    default code    */
  195.                         font = defaultfont;
  196.                         for (i=0; i<fonts; i++)
  197.                             if (strcmp(fontname[i],fname) == 0 &&
  198.                                 pointsize[i] == fsize) {
  199.                                     *cmdptr = i;
  200.                                     font = tf[i];
  201.                                     }
  202.                         cmdptr++;
  203.                         }
  204.                     else {
  205.                         cmdsiz += 2;
  206.                         if (fonts >= MAXFONTS) {
  207.                             if (runningfromcli) {
  208.                                 sprintf(buff,"Too many fonts:-\n%s\n",line);
  209.                                 print(buff);
  210.                                 }
  211.                             continue;
  212.                             }
  213.                         strcpy(fontname[fonts],fname);
  214.                         pointsize[fonts] = fsize;
  215.                             
  216.                         if (fontname[fonts][0] != '\0' 
  217.                             && pointsize[fonts]) {
  218.                                 ++fonts;
  219.                             /*  check for duplications  */
  220.                                 {
  221.                                 for (i=0; i<(fonts-1); i++)
  222.                                     if(strcmp(fontname[i],
  223.                                         fontname[fonts-1]) == 0 &&
  224.                                         pointsize[i] == pointsize[fonts-1])
  225.                                             --fonts;
  226.                                 }
  227.                             }
  228.                         else {
  229.                             if (runningfromcli) {
  230.                                 sprintf("Corrupt FNT command:-\n%s\n",line);
  231.                                 print(buff);
  232.                                 }
  233.                             continue;
  234.                             }
  235.                         }
  236.                         
  237.                     continue;
  238.  
  239. /*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    */
  240.  
  241.                 case 5749:         /*  IND: set left margin        */
  242.                     if (pass) {
  243.                         p = strtok(stpblk(p)," ;");
  244.                         if (p != NULL && isdigit((int)*p)) {
  245.                             ind = atoi(p);
  246.                             if (ind > width - 2)
  247.                                 ind = 0;
  248.                             }
  249.                         else
  250.                             if (runningfromcli) {
  251.                                 sprintf(buff,
  252.                                     "Corrupt IND command:-\n%s\n",line);
  253.                                 print(buff);
  254.                                 }
  255.                         }
  256.                         
  257.                     continue;
  258.  
  259. /*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    */
  260.                 case 7792:      /*  LNS: draw line(s)            */
  261.                     if (pass) {
  262.                         *cmdptr++ = 2;  /*  command code    */
  263.                         getargs(3,p);
  264.                         }
  265.                     else {
  266.                         cmdsiz += 4;
  267.                         no_text_or_lines = 0;
  268.                         }
  269.                     continue;
  270.  
  271. /*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    */
  272.  
  273.                 case 9596:      /*  OFC: turn off centring     */
  274.                     if (pass)
  275.                         centring = 0;
  276.                     continue;
  277.  
  278. /*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    */
  279.  
  280.                 case 9804:      /*  ONC: turn on centring      */
  281.                     if (pass)
  282.                         centring = 1;
  283.                     continue;
  284.  
  285. /*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    */
  286.  
  287.                 case 10445:     /*  PLT: change pallette        */
  288.                     if (pass) {
  289.                         *cmdptr++ = 10; /*  command code    */
  290.                         getargs(4,p);
  291.                         }
  292.                     else
  293.                         cmdsiz += 5;
  294.  
  295.                     continue;
  296.  
  297. /*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    */
  298.  
  299.                 case 11979:      /*  RST: replay from start        */
  300.                     if (pass)
  301.                         *cmdptr++ = 14;  /*  command code    */
  302.                     else
  303.                         cmdsiz += 1;
  304.  
  305.                     continue;
  306.  
  307. /*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    */
  308.  
  309.                 case 12401:     /*  SIZ: screen size change     */
  310.                     if (pass) {
  311.                         *cmdptr++ = 11; /*  command code    */
  312.                         p = stpblk(p);
  313.                         if (strnicmp(p,"HIRES",5) == 0)
  314.                             *cmdptr++ = 1;
  315.                         else if (strnicmp(p,"LORES",5) == 0)
  316.                             *cmdptr++ = 0;
  317.                         else {
  318.                             *cmdptr++ = 1;
  319.                             if (runningfromcli) {
  320.                                 sprintf(buff,
  321.                                     "Not 'HIRES' or 'LORES' :-\n%s\n",line);
  322.                                 print(buff);
  323.                                 }
  324.                             }
  325.                         p = strpbrk(p,", ");
  326.                         
  327.                         getargs(5,p);
  328.                         
  329.                         width = *(cmdptr-5);
  330.                         height = *(cmdptr-4);
  331.                         
  332.                         if(checksizes(&width,&height,
  333.                             cmdptr-3,cmdptr-2,cmdptr-1))
  334.                             *(cmdptr-6) = 1;    /*    must be HIRES    */
  335.  
  336.                         *(cmdptr-5) = width;
  337.                         *(cmdptr-4) = height;
  338.  
  339.                         }
  340.                     else
  341.                         cmdsiz += 7;
  342.  
  343.                     continue;
  344.  
  345. /*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    */
  346.  
  347.                 case 12560:     /*  SPC: change text spacing    */
  348.                     if (pass) {
  349.                         *cmdptr++ = 9;  /*  command code    */
  350.                         getargs(1,p);
  351.                         }
  352.                     else
  353.                         cmdsiz += 2;
  354.  
  355.                     continue;
  356.  
  357. /*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    */
  358.  
  359.                 case 12561:     /*  SPD: change scroll speed    */
  360.                     if (pass) {
  361.                         *cmdptr++ = 6;  /*  command code    */
  362.                         getargs(1,p);
  363.                         }
  364.                     else
  365.                         cmdsiz += 2;
  366.  
  367.                     continue;
  368.  
  369. /*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    */
  370.  
  371.                 case 12661:      /*  SSZ: smooth sizing            */
  372.                     if (pass) {
  373.                         *cmdptr++ = 12;  /*  command code    */
  374.                         getargs(7,p);
  375.  
  376.                         width = *(cmdptr-5);
  377.                         height = *(cmdptr-4);
  378.  
  379.                         checksizes(&width,&height,
  380.                                     cmdptr-3,cmdptr-2,cmdptr-1);
  381.  
  382.                         *(cmdptr-5) = width;
  383.                         *(cmdptr-4) = height;
  384.  
  385.                         }
  386.                     else
  387.                         cmdsiz += 8;
  388.  
  389.                     continue;
  390.  
  391. /*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    */
  392.  
  393.                 case 12845:     /*  TAB: set tab expansion    */
  394.                     p = strtok(stpblk(p)," ;");
  395.                     if (p != NULL && isdigit((int)*p)) {
  396.                         tab = atoi(p);
  397.                         if (tab > MAXLINE - 2)
  398.                             tab = 0;
  399.                         }
  400.                     else {
  401.                         tab = DEFAULTTABSIZE;
  402.                         if (pass && runningfromcli) {
  403.                             sprintf(buff,"Corrupt TAB command:-\n%s\n",line);
  404.                             print(buff);
  405.                             }
  406.                         }
  407.  
  408.                     continue;
  409.                 
  410. /*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    */
  411.  
  412.                 default:
  413.                     if (!pass && runningfromcli) {
  414.                         sprintf(buff,"Bad code: Value: %d\n%s",j,line);
  415.                         print(buff);
  416.                         }
  417.  
  418.                     continue;
  419.  
  420. /*    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    *    */
  421.  
  422.                 }    /*  end of switch    */
  423.                 
  424.             }    /* end of if    */
  425.             
  426.         else {
  427.  
  428.             /*  handle lines NOT starting with escape code i.e. text    */
  429.  
  430.             /*    expand tabs    */
  431.             if (strchr(line,'\t')) {
  432.  
  433.                 char copy[MAXLINE + 1],last;
  434.                 register char *p, *t;
  435.                 register int n;
  436.                 int i;
  437.  
  438.                 t = strcpy(copy,line); 
  439.                 p = line;
  440.  
  441.                 for (n = 1; (last = *p++ = *t++) && (n <= MAXLINE); ++n) {
  442.                     if (last == '\t') {
  443.                         n--;
  444.                         p--;
  445.                         if (tab)
  446.                             for (i = tab - n % tab; i>0; i--) {
  447.                                 *p++ = ' ';
  448.                                 n++;
  449.                                 if (n >= MAXLINE) break;
  450.                                 }
  451.                         }
  452.                     }
  453.  
  454.                 if (n >= MAXLINE)
  455.                     strcpy (line,copy);
  456.                 }
  457.  
  458.             if (pass) {
  459.                 *cmdptr++ = 1;
  460.                 *cmdptr++ = strlen(line)-1;
  461.  
  462.                 if (centring) {
  463.                     SetFont(&rp,font);
  464.                     /*    print text at middle window height    */
  465.                     Move(&rp,0,30+(font->tf_Baseline * 2 - font->tf_YSize)/2);
  466.                     Text(&rp,line,*(cmdptr-1));
  467.                     *cmdptr = (width - rp.cp_x)/2;
  468.                     if (*cmdptr < 0 || *cmdptr > width/2)
  469.                         *cmdptr = 0;
  470.                     ++cmdptr;
  471.                     }
  472.                 else
  473.                     *cmdptr++ = ind;
  474.  
  475.                 if(strlen(line) > 1)
  476.                     txptr = strncpy(txptr,line,strlen(line)-1);
  477.                 txptr += strlen(line)-1;
  478.                 }
  479.             else {
  480.                 cmdsiz += 3;
  481.                 textsiz += strlen(line)-1;
  482.                 no_text_or_lines = 0;
  483.                 }
  484.  
  485.             }
  486.         
  487.         }    /* while    */
  488.  
  489. }    /*  end of parse    */
  490.  
  491.  
  492. void getargs(number,p)
  493. int number;
  494. char *p;
  495.  
  496. {
  497.     char *t;
  498.     int fail;
  499.     
  500.     strcpy(buff,line);  /*  take copy 'cos strtok() changes string  */
  501.     
  502.     fail = 0;
  503.  
  504.     if (--number) 
  505.         t = strtok(stpblk(p)," ,");
  506.     else
  507.         t = strtok(stpblk(p)," ;");
  508.         
  509.     if (t != NULL && isdigit(*t))
  510.         *cmdptr++ = (short) atoi(t);
  511.     else {
  512.         *cmdptr++ = 0;
  513.         fail = 1;
  514.         }
  515.     while (number--)
  516.         if (fail)
  517.             *cmdptr++ = 0;
  518.         else {
  519.             if (number)
  520.                 t = strtok(NULL," ,");
  521.             else
  522.                 t = strtok(NULL," ;");
  523.  
  524.             if (t != NULL && *t == '-' || isdigit(*t))
  525.                 *cmdptr++ = (short) atoi(t);
  526.             else {
  527.                 *cmdptr++ = 0;
  528.                 fail = 1;
  529.                 }
  530.             }
  531.     if (fail && runningfromcli) {
  532.         strcpy(line,buff);
  533.         sprintf(buff,"Corrupt command:-\n%s\n",line);
  534.         print(buff);
  535.         }
  536.  
  537. }    /*    end of getargs    */
  538.  
  539. int checksizes(w,h,x,y,ox)
  540.  
  541. int   *w,*h;
  542. short *x,*y,*ox;
  543.  
  544. {
  545.     int hires = 0;
  546.  
  547.     if (*w > MAX_WIDTH)
  548.         *w = MAX_WIDTH;
  549.  
  550.     if (*w < MIN_WIDTH)
  551.         *w = MIN_WIDTH;
  552.  
  553.     if (*w > 352)
  554.         hires=1;    /*    must be HIRES    */
  555.  
  556.     if (*h > MAX_HEIGHT)
  557.         *h = MAX_HEIGHT;
  558.  
  559.     if (*h < MIN_HEIGHT)
  560.         *h = MIN_HEIGHT;
  561.  
  562.     if (*w > maxwidth)
  563.         maxwidth = *w;
  564.  
  565.     if (*h > maxheight)
  566.         maxheight = *h;
  567.  
  568.     if (*x > 703)
  569.         *x = 703;
  570.  
  571.     if (*x > 351)
  572.         hires=1;    /*    must be HIRES    */
  573.  
  574.     if (*y > 200)
  575.         *y = 200;
  576.  
  577.     if (*x < -16)
  578.         *x = -16;
  579.  
  580.     if (*y < -16)
  581.         *y = -16;
  582.  
  583.     if (*ox < 0)
  584.         *ox = 0;
  585.  
  586.     if (*ox > 640)
  587.         *ox = 640;
  588.     
  589.     return (hires);
  590. }